package com.summer.data.springboot;

import com.summer.data.rs.datatype.Page;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;

@JsonComponent
public class PageJsonSerializer extends StdSerializer<Page> {

    protected PageJsonSerializer() {
        super(Page.class);
    }

    @Override
    public void serialize(Page page, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeObjectField("page_no", page.getPageNo());
        jsonGenerator.writeObjectField("page_size", page.getPageSize());
        jsonGenerator.writeObjectField("record_count", page.getRecordCount());

        jsonGenerator.writeObjectField("data", page.getData());

        jsonGenerator.writeObjectField("pageNo", page.getPageNo());
        jsonGenerator.writeObjectField("pageSize", page.getPageSize());
        jsonGenerator.writeObjectField("recordCount", page.getRecordCount());

        jsonGenerator.writeObjectField("_page_no", page.getPageNo());
        jsonGenerator.writeObjectField("_page_size", page.getPageSize());
        jsonGenerator.writeObjectField("_record_count", page.getRecordCount());

        jsonGenerator.writeEndObject();
    }
}
